//
//  WBNetworkManager+WBExtention.m
//  TransWB
//
//  Created by 孝飞王 on 2016/12/30.
//  Copyright © 2016年 孝飞王. All rights reserved.
//

#import "WBNetworkManager+WBExtention.h"
//封装新浪微博的你滚落请求方法
@implementation WBNetworkManager (WBExtention)

- (void)statustListParam:(NSDictionary*)param completion:(void(^)( NSArray  * list  , BOOL isSuccess))completion{
    
    //添加网络加载 
    NSString *urlString  = @"https://api.weibo.com/2/statuses/home_timeline.json";
    [self tokenRequestHttpMethod:GET urlString:urlString parameters:param completion:^(id responseObject, BOOL isSuccess) {
        completion(responseObject[@"statuses"],isSuccess);
    }];
}

- (void)unreadBadgetComplet:(void(^)(NSInteger count))complet{
    
    NSString *urlString = @"https://rm.api.weibo.com/2/remind/unread_count.json";
    NSDictionary *param  =@{@"uid":@"4058823188830591"};

    [self tokenRequestHttpMethod:GET urlString:urlString parameters:param completion:^(id responseObject, BOOL isSuccess){
       NSInteger  count = (NSInteger)responseObject[@"status"];
        complet(count);
    }];
}

//获取touken oAthou
- (void)loadAccessToken:(NSString*)code completion:(void(^)(BOOL isSuccess))completion{
    
    NSString *urlString = @"https://api.weibo.com/oauth2/access_token";
    NSDictionary *param = @{
                                @"client_id":App_Key,
                                @"client_secret":App_Secret,
                                @"grant_type":@"authorization_code",
                                @"code":code,
                                @"redirect_uri":back_Url,
                                };
    
    [[WBNetworkManager shareManager] requestHTTPMethod:POST urlString:urlString parameters:param completion:^(id responseObject, BOOL isSuccess) {
        
        NSLog(@"%@",responseObject);
        if(responseObject == nil)
            responseObject = @{};
        
       // [self.userAccount yy_modelSetWithJSON:responseObject];
        [self.userAccount yy_modelSetWithDictionary:responseObject];
        [self.userAccount saveAccount];
        
        //加载用户信息
        [self  loadUserInfo:^(NSDictionary *dict) {
            
            //设置用户字典
            [self.userAccount yy_modelSetWithDictionary:dict];
            
            //用户加载完成  完成回调
            completion(isSuccess);
        }];
        

        
    }];
}

//加载用户信息  登录够立即执行
-(void)loadUserInfo:(void(^)(NSDictionary *dict))completion{
    
    //如果没有uid 不请求
    if (!self.userAccount.uid)    return;
    NSString *urlString = @"https://api.weibo.com/2/users/show.json";
    NSDictionary *param = @{@"uid":self.userAccount.uid};
    
    [self tokenRequestHttpMethod:GET urlString:urlString parameters:param completion:^(id responseObject, BOOL isSuccess) {
        NSLog(@"用户信息==== %@",responseObject);
        completion(responseObject);
    }];
    
}


@end
